﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Tools.Publishing;
using $safeprojectname$.Infrastructure.Factories;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace $safeprojectname$.Core.Publishing.Configuration
{
    [TestClass]
    public class CreatingProviderTest : TestBase
    {
        [TestMethod]
        public void AppConfig_Passed()
        {
            var provider = ProviderFactory.ProviderByAppConfig() as DefaultPublishingProvider;
            Assert.IsTrue(provider.Is());
        }

        [TestMethod]
        public void NullProvider_Passed()
        {
            var provider = ProviderFactory.NullProvider() as NullPublishingProvider;
            Assert.IsTrue(provider.Is());
        }

        //[TestMethod]
        public void DefaultProvider_Passed()
        {
            var provider = ProviderFactory.DefaultProvider() as DefaultPublishingProvider;
            Assert.IsTrue(provider.Is());
        }

        [TestMethod]
        public void ProviderLog4NetOnly_Passed()
        {
            var provider = ProviderFactory.ProviderLog4NetOnly() as DefaultPublishingProvider;
            var handler = provider.Handler as Log4NetPublishingHandler;
            Assert.IsTrue(handler.Is());
        }

        //[TestMethod]
        public void ProviderExceptionOnly_Passed()
        {
            var provider = ProviderFactory.ProviderExceptionOnly() as DefaultPublishingProvider;
            var handler = provider.Handler as ExceptionPublishingHandler;
            Assert.IsTrue(handler.Is());
        }

        [TestMethod]
        public void Provider_IsMissing_Failed()
        {
            ConfigurationErrorsException exception = null;
            try
            {
                var provider = ProviderFactory.WithMissingProvider() as DefaultPublishingProvider;
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Is());
        }

        [TestMethod]
        public void WrongProviderType_Failed()
        {
            ConfigurationErrorsException exception = null;
            try
            {
                var provider = ProviderFactory.WithWrongProviderType() as DefaultPublishingProvider;
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Message.Contains("contains object, which does not implement 'IPublishingProvider' interface."), exception.Message);
        }

        [TestMethod]
        public void ProviderTypeBroken_Failed()
        {
            ConfigurationErrorsException exception = null;
            try
            {
                var provider = ProviderFactory.ProviderTypeBroken() as DefaultPublishingProvider;
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Is());
        }

        [TestMethod]
        public void ProviderWithNullConfig_Passed()
        {
            var provider = ProviderFactory.ProviderWithNullConfig() as NullPublishingProvider;

            Assert.IsTrue(provider.Is());
        }

        [TestMethod]
        public void BrokenProvider_Failed()
        {
            ConfigurationErrorsException exception = null;
            try
            {
                var provider = ProviderFactory.BrokenProvider() as DefaultPublishingProvider;
            }
            catch (ConfigurationErrorsException ex)
            {
                exception = ex;
            }
            Assert.IsTrue(exception.Is());
        }

        #region Init

        // You can use the following additional attributes as you write your tests:

        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }

        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }

        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion Init
    }
}
